package view;

import org.jb2011.lnf.beautyeye.ch3_button.BEButtonUI;
import services.Service;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.Map;

public class ModifyOrderProductsJFrame extends JFrame implements MouseListener {
    // 定义全局组件
    JPanel backgroundPanel, labelPanel, contentPanel, buttonPanel;
    JLabel labelName, labelAmount;
    JTextField amount;
    JComboBox name;
    JButton buttonAdd;

    Service service;
    ArrayList<String> productsIds;
    ArrayList<Integer> amounts;
    public Map<String,String> productsMap = null;
    // 父面板对象
    AddOrderJFrame parentJFrame;
    JTable table;
    int row;
    public ModifyOrderProductsJFrame(Service service, ArrayList<String> productsIds,ArrayList<Integer> amounts,
                                     AddOrderJFrame addOrderJFrame, JTable table, int row) throws Exception {
        this.service = service;
        this.parentJFrame = addOrderJFrame;
        this.productsIds = productsIds;
        this.amounts = amounts;
        this.table = table;
        this.row = row;
        productsMap = service.selectAllIdName(0);
        initBackgroundPanel();

        this.add(backgroundPanel);

        this.setTitle("修改产品");
        this.setSize(640, 240);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
    }
    // 初始化背景面板
    public void initBackgroundPanel() throws Exception {
        backgroundPanel = new JPanel(new BorderLayout());

        initLabelPanel();
        initContentPanel();
        initButtonPanel();

        backgroundPanel.add(labelPanel, "North");
        backgroundPanel.add(contentPanel, "Center");
        backgroundPanel.add(buttonPanel, "South");
    }
    // 初始化label面板
    public void initLabelPanel() {

        labelPanel = new JPanel();

        JLabel title = new JLabel("产品信息");
        title.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        labelPanel.add(title);
    }
    // 初始化商品信息面板
    public void initContentPanel() throws Exception {
        contentPanel = new JPanel(new GridLayout(2, 2));

        labelName = new JLabel("产品名称", JLabel.CENTER);
        labelAmount = new JLabel("产品数量", JLabel.CENTER);

        // 商品种类下拉框
        name = new JComboBox();
        if (productsMap != null) {
            int sign = 0;
            int  i=0;
            for (String s : productsMap.keySet()) {
                if (s.equals(table.getValueAt(row, 1))) {
                    sign = i;
                    i++;
                }
                System.out.println(s);
                name.addItem(s+" | 余: "+service.selectByName(0,s).get(0).get(3));
            }
            name.setSelectedIndex(sign);
        }

        amount = new JTextField((String) table.getValueAt(row, 7));

        contentPanel.add(labelName);
        contentPanel.add(name);
        contentPanel.add(labelAmount);
        contentPanel.add(amount);
    }

    // 初始化按钮面板
    public void initButtonPanel() {
        buttonPanel = new JPanel();

        buttonAdd = new JButton("保存");
        buttonAdd.setUI(new BEButtonUI().setNormalColor(BEButtonUI.NormalColor.lightBlue));
        buttonAdd.setForeground(Color.white);
        buttonAdd.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        buttonAdd.addMouseListener(this);

        buttonPanel.add(buttonAdd);
    }
    @Override
    public void mouseClicked(MouseEvent e) {
        if (e.getSource() == buttonAdd) {
            String nameString = (String) name.getSelectedItem();
            String amountString = amount.getText().trim();
            if (amountString.isEmpty()) {
                JOptionPane.showMessageDialog(null, "请输入产品数量");
            } else {
                int amountInt = Integer.parseInt(amountString);
                int remain = Integer.parseInt(nameString.split(" ")[3]);
                if(amountInt>remain){
                    JOptionPane.showMessageDialog(null, "库存不足");
                }else {
                    productsIds.add(productsMap.get(nameString.split(" ")[0]));
                    amounts.add(Integer.parseInt(amountString));

                    JOptionPane.showMessageDialog(null, "修改成功");
                    this.setVisible(false);
                    parentJFrame.refreshTablePanel();
                }
            }
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {

    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
